
 
capture program drop figure_chen
program define figure_chen

global bin=10
global binname=$bin
global data_in=20
global data_out=1180
global range             "RC>=float(${data_in}) & RC<=float(${data_out})"
global range_rv          "incbin${binname}>= float(${data_in}+${bin}) & incbin${binname}<=float(${data_out}+${bin})"

use "${processed_data}\trading_rights_asif.dta" , clear 
rename $myvar y

keep      if  ${firm}==1 & ${condition}  &  ${year_condition}  & ${range} 

gen tinctobin= RC/${bin}  //registered capital; unit 10K RMB
gen incbinaux=floor(tinctobin)
gen incbin${binname}=incbinaux*${bin}
drop  tinctobin   incbinaux
keep if ${range_c300}

gen RC_2=RC*RC
gen RC_3=RC_2*RC
gen D=0
replace D=1 if incbin${binname}>=float(${cutoff})
*rounding
gen round50=0
replace round50=1  if mod(incbin${binname},50)==0
gen round100=0
replace round100=1  if  mod(incbin${binname},100)==0  
gen round50X  =round50*RC
gen round50X2 =round50*RC_2
gen round100X =round100*RC
gen round100X2=round100*RC_2
global rounding_q  "round50 round100 round50X  round100X  round50X2 round100X2"
global rounding_l  "round50 round100 round50X  round100X "
keep y id year incbin* RC*  D  $rounding_q  cic_adj2 


gen DRC=D*RC
gen DRC_2=D*RC_2 
gen DRC_3=D*RC_3  

reg y  D DRC  DRC_2 DRC_3   RC  RC_2  RC_3 
predict y_fit   
global bin_new=$bin * 2 
gen tinctobin= incbin${binname}/${bin_new} //draw figure at bin=20
gen incbinaux=floor(tinctobin)
gen incbin${bin_new}=incbinaux*${bin_new}
drop  tinctobin   incbinaux

collapse (count) numbin${bin_new}= year (mean) y=y  y_fit=y_fit  , by (incbin${bin_new}) 


#delimit;
qui twoway 
(scatter y incbin${bin_new}[fweight=numbin${bin_new}], mcolor(green  green) msize(small small) ) 
(line  y_fit  incbin${bin_new} if incbin${bin_new}<float(${cutoff}),  sort clcolor(black)  mcolor(black) msymbol(o) lwidth(thick) lpattern(solid))
(line  y_fit  incbin${bin_new} if incbin${bin_new}>=float(${cutoff}) , sort clcolor(black)  mcolor(black) msymbol(o) lwidth(thick) lpattern(solid))
,
legend(order(1 "Observed" 2 "Fitted" )    size(small) )
xline(${cutoff}, lcolor(black) lwidth(medium))		
ytitle(${myvar_name}, size(small)  margin(medium))  
xtitle(Registered Capital (unit:10,000 RMB) in ${year_name}, size(small) margin(medium) ) xtitle(, alignment(top)) 						
xlabel(${zoom_in_c300} (20) ${zoom_out_c300}, labsize(small))			
ylabel(#5) 
ylabel(0(5000)20000)
graphregion(fcolor(white)  ifcolor(white) color(white) icolor(white))
title("${myvar_name}", justification(center) color(black) size(medsmall)) 
subtitle( "for ${firm_name} firms in ${location} ${year_note}"  "${cutoff_note}",  justification(center) color(black) size(medidum))
note("Note: data: ASIF.",size(small) )
;						
graph export   "${causal_graph_dir}/fig_chen_${firm_name}_${name}_${year_name}_${myvar}.pdf"
,replace;
#delimit cr 

end